<?php
  
  class ftp {
      
      
  public $host = "";
  public $user = "";
  public $password = "";
  public $path = "";
      
      
  private $ftpCon;    
      
      
      
      
      /**
      * connect to ftp server
      * 
      */
      function connect() {
          
          $result = true;
          
          if (!$this->ftpCon = ftp_connect($this->host)) {
            
            echo "fehler bei ftp connection\n";
            
            echo "FTP Server: ".$this->host."\n";
            echo "FTP User: ".$this->user."\n";
            echo "FTP Password: ".$this->password."\n";
            
            
            
            
            
            exit;
              $result = false;
              
              
          } 

          
          
            echo "FTP Server: ".$this->host."\n";
            echo "FTP User: ".$this->user."\n";
            echo "FTP Password: ".$this->password."\n";
            
          
          
          // send access parameters
if (ftp_login($this->ftpCon, $this->user, $this->password) == false) {
    
    echo "fehler bei ftp login\n";
    
    echo "FTP Server: ".$this->host."\n";
            echo "FTP User: ".$this->user."\n";
            echo "FTP Password: ".$this->password."\n";
            
    
    exit;
    $result = false;
    
} 

          return $result;
          
      }
      
      
      
      
      
      
      
      
                       // $sourceFilename
   function uploadFile($filename, $destinationPath) {   
      

// open connection
//$conn = ftp_connect($this->host) or die ("Cannot initiate connection to host");
// send access parameters
//ftp_login($this->ftpCon, $this->user, $this->password) or die("Cannot login");
// perform file upload

global $rootDir;

$upload = ftp_put($this->ftpCon, $this->path.$destinationPath."/".file::getFilename($filename), $filename, FTP_BINARY);
// check upload status
// display message
if (!$upload) {
echo "Cannot upload";
} else {
echo "Upload complete";
}
// close the FTP stream
//ftp_close($conn);
// delete local copy of uploaded file
//unlink($workDir."/".$tmpName) or die("Cannot delete uploaded
//file from working directory -- manual deletion recommended");
      
       
  }
  
  
  
  function renameFile($filenameFrom, $filenameTo) {
      
      if (ftp_rename($this->ftpCon, $this->path.$filenameFrom, $this->path.$filenameTo)) {
// echo "$old_file erfolgreich in $new_file umbenannt\n";
} else {
 echo "Ein Fehler trat beim Umbenennen von $filenameFrom in $filenameTo auf\n";
}
      
      
      
  }
  
  
  
  function uploadFolder($sourcePath, $destinationPath = "") {
      
      
      // auf passiv modus wechseln
      ftp_pasv($this->ftpCon, true);
      
      
      echo "source path $sourcePath";
      //$sourcePath = 'C:\daten\catch24/distribution';
      
      foreach (folder::getFolder($sourcePath, true, true, true, false) as $folder) {
          
          
          if (file::isDir($folder['dir'], false)) {
            
              //$sourcePath
              
              $ftpFolder = $folder['dir'];
              $ftpFolder = str_ireplace($sourcePath, "", $ftpFolder);
              $ftpFolder = $this->path.$destinationPath.$ftpFolder;
              
              
              //ftp_mkdir($this->ftpCon, $destinationPath."/".$folder['filename']);      
              ftp_mkdir($this->ftpCon, $ftpFolder);        
              
          }
          
          if (file::isFile($folder['dir'], false)) {
          
          //print_r($folder);
           $ftpFilename = $folder['dir'];
            $ftpFilename = str_ireplace($sourcePath, "", $ftpFilename);
              
           $ftpFilename = $this->path.$destinationPath.$ftpFilename;
           
           
           
           //$upload = ftp_put($this->ftpCon, $folder['dir'], $destinationPath . '/' . $folder['filename'], FTP_BINARY);
           //$upload = ftp_put($this->ftpCon, $folder['dir'], $ftpFilename, FTP_BINARY);
           //$upload = ftp_put($this->ftpCon, $ftpFilename,$folder['dir'], FTP_BINARY);
           
           
           echo "Ftp Filename: $ftpFilename\n";
           echo "Local Filename: ".$folder['dir']."\n";
           //$upload = ftp_put($this->ftpCon, $ftpFilename,$folder['dir'], FTP_ASCII);
           $upload = ftp_put($this->ftpCon, $ftpFilename,$folder['dir'], FTP_BINARY);
           
           
           
           
           
           
           //echo $folder['dir']."\n";
           //echo $ftpFilename;
           //exit;
                
          }
          
          //exit;
          
          
      }
      
      
      
      
    // function recurse_copy($src,$dst) { 
  /*  $dir = opendir($sourcePath); 
    //mkdir($destinationPath); 
    
    
    while(false !== ( $file = readdir($dir)) ) { 
        if (( $file != '.' ) && ( $file != '..' )) { 
            if ( is_dir($sourcePath . '/' . $file) ) { 
               
                //mkdir($destinationPath . '/' . $file);
                //ftp_mkdir($this->ftpCon, $destinationPath."/".$file);
                
                
       //         exit;
                
     //           $this->uploadFolder($sourcePath . '/' . $file, $destinationPath."/".$file);
                
                //file::copyFolder($sourcePath . '/' . $file,$destinationPath . '/' . $file); 
                
                global $rootDir;

//$upload = ftp_put($ftpCon, $this->path."/".file::getFilename($filename), $filename, FTP_BINARY);
                
                
            } 
            else { 
                //copy($sourcePath . '/' . $file,$destinationPath . '/' . $file); 
                
                global $rootDir;

                //$upload = ftp_put($this->ftpCon, $destinationPath."/".file::getFilename($file), $sourcePath . '/' . $file, FTP_BINARY);
                echo "mkdir ". $destinationPath."/".file::getFilename($file)."\n"; 
                
            } 
        } 
    } 
    closedir($dir); */  
      
      
      
      
  }
  
  
  
  
  
  /**
  * Close Ftp Connection
  * 
  */
  
  function disconnect() {
      
     ftp_close($this->ftpCon);   
      
      
  }
  
  
  
  
  
  
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
?>